package com.dsprun.dspai.controller;


import com.dsprun.dspai.dto.AgentCommandRequest;
import com.dsprun.dspai.services.AiAgentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * AI服务控制器，提供RESTful API接口
 */
@RestController
@RequestMapping("/agent/")
@RequiredArgsConstructor
public class AiAgentController {
    private final AiAgentService aiAgentService;


    @PostMapping("/agent")
    @Operation(
            summary = "智能体指令",
            description = "通过自然语言指令调用系统功能",
            responses = {
                    @ApiResponse(
                            description = "指令执行结果",
                            content = @Content(mediaType = "text/plain")
                    ),
                    @ApiResponse(responseCode = "400", description = "无效指令格式")
            }
    )
    public String agentCommand(
            @Parameter(description = "自然语言指令", required = true,
                    example = "请把'你好'翻译成英文")
            @RequestBody AgentCommandRequest request) {
        return aiAgentService.executeAgentCommand(request.getCommand());
    }

    @PostMapping("/agent/with-memory")
    @Operation(
            summary = "带记忆的智能体",
            description = "通过会话ID保持上下文的智能体交互",
            responses = {
                    @ApiResponse(
                            description = "执行结果",
                            content = @Content(
                                    mediaType = "application/json",
                                    schema = @Schema(implementation = AgentResponse.class)
                            )

                    )
            })
   public AgentResponse agentWithMemory(
            @Parameter(description = "会话请求", required = true)
            @RequestBody AgentMemoryRequest request) {

            String result = aiAgentService.executeAgentCommandWithMemory(
            request.getSessionId(),
            request.getCommand()
    );

    return new AgentResponse(result);
}

// 请求类
@Data
static class AgentMemoryRequest {
    @Schema(description = "会话ID", example = "sess_123", required = true)
    private String sessionId;

    @Schema(description = "自然语言指令", example = "继续翻译成法语", required = true)
    private String command;
}

// 响应类
@Data
@AllArgsConstructor
static class AgentResponse {
    @Schema(description = "执行结果", example = "Bonjour le monde")
    private String result;
}

}
